package com.fieldchain.ex.fa100.ui.access;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseFragment;
import com.fieldchain.ex.fa100.bean.CoinsBean;
import com.fieldchain.ex.fa100.ui.money.adapter.MoneyAdapter;
import com.fieldchain.ex.fa100.ui.personal.activity.PersonalCanterActivity;
import java.util.ArrayList;
import java.util.List;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.BeanRequest;
import com.fieldchain.ex.fa100.utils.string.NumberFormate;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/16
 */
public class MoneyFragment extends MyBaseFragment {
    private ImageView ivPersonal;
    private ImageView tvTitle;
    private ImageView ivTop;
    private TextView tvM1;
    private TextView tvM2;
    private ImageView ivRefresh;
    private RecyclerView rv;
    private RelativeLayout reaEmpty;
    private TextView tvEmpty;
    private List<CoinsBean.CoinListBean> mList = new ArrayList<>();
    private RotateAnimation animation;


    private MoneyAdapter adapter;

    @Override
    protected void findView() {
        ivPersonal = view.findViewById(R.id.iv_personal);
        tvTitle = view.findViewById(R.id.tv_title);
        ivTop = view.findViewById(R.id.iv_top);
        tvM1 = view.findViewById(R.id.tv_m1);
        tvM2 = view.findViewById(R.id.tv_m2);
        ivRefresh = view.findViewById(R.id.iv_refresh);
        rv = view.findViewById(R.id.rv);
        reaEmpty = view.findViewById(R.id.rea_empty);
        tvEmpty = view.findViewById(R.id.tv_empty);
    }

    @Override
    protected void setValue() {
        tvEmpty.setText("");
        adapter = new MoneyAdapter(mContext);
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setAdapter(adapter);


        animation = new RotateAnimation(0, 360*4, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(1200);
        animation.setInterpolator(new DecelerateInterpolator());

        ivRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ivRefresh.startAnimation(animation);
                getData();
            }
        });
        ivPersonal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext, PersonalCanterActivity.class));
            }
        });

    }

    @Override
    public void onPause() {
        super.onPause();
        if (animation != null) {
            animation.cancel();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(mContext!=null)
            Log.e("onResume1",getUserVisibleHint()+"   "+getClass().getSimpleName());
        getData();
    }

    @Override
    public int getLayout() {
        return R.layout.fragment_money;
    }

    private void getData() {
        HttpRequest.getInstance().getRequest(Constant.assets,new BeanRequest<CoinsBean>() {
            @Override
            public void onSucess(CoinsBean coinsBean, int code, String msg) {
                if(code==200){
                    mList.clear();
                    mList.addAll(coinsBean.getCoinList());
                    adapter.setData(mList);
                    tvM1.setText(NumberFormate.m3(coinsBean.getTotalAssetsCny())+"");
                    tvM2.setText(NumberFormate.m3(coinsBean.getTotalAssetsUsd())+"");
                }
                if (mList.size() == 0) {
                    reaEmpty.setVisibility(View.VISIBLE);
                } else {
                    reaEmpty.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFaild(Exception e) {
                reaEmpty.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void loginShow() {
        super.loginShow();
        Log.e("login","loginShow");
    }
}
